﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VOL;
using BLL;

namespace Shopper
{
    public partial class frmShopping : DevExpress.XtraEditors.XtraForm
    {
        private DMHangBL hangBL = new DMHangBL();
        private DMKhachBL khachBL = new DMKhachBL();
        private DataTable dtCart = new DataTable();

        private float tongTien = 0;

        public frmShopping()
        {
            InitializeComponent();
        }

        private void frmShopping_Load(object sender, EventArgs e)
        {
            ResetForm();
        }

        private void btnSelectHangHoa_Click(object sender, EventArgs e)
        {
            int MaHang = (int)cbHangHoa.EditValue;
            DMHang obj = new DMHang();
            obj.MaHang = MaHang;

            // cờ tồn tại hay chưa
            bool exist_flg = false;
            foreach (DataRow r in dtCart.Rows)
            {
                if (r["MaHang"].ToString().Equals(MaHang.ToString()))
                {
                    exist_flg = true;
                    r["SoLuong"] = int.Parse(r["SoLuong"].ToString()) + 1;
                    r["ThanhTien"] = int.Parse(r["SoLuong"].ToString()) * float.Parse(r["DonGia"].ToString());
                }
                
            }

            if (exist_flg == false)
            {
                DataTable dt = hangBL.searchIDDMHang(obj);

                DataRow row = dtCart.NewRow();
                row["MaHang"] = dt.Rows[0]["MaHang"];
                row["TenHang"] = dt.Rows[0]["TenHang"];
                row["DVT"] = dt.Rows[0]["DVT"];
                row["MaNhaCungCap"] = dt.Rows[0]["MaNhaCungCap"];
                row["MaLoaiHang"] = dt.Rows[0]["MaLoaiHang"];
                row["DonGia"] = dt.Rows[0]["DonGia"];
                row["SoLuong"] = 1;
                row["ThanhTien"] = row["DonGia"];
                dtCart.Rows.Add(row);
            }
            FillData();
        }

        private void FillData()
        {
            gridControl1.DataSource = dtCart;

            tongTien = 0;
            foreach (DataRow r in dtCart.Rows)
            {
                tongTien += float.Parse(r["ThanhTien"].ToString());
            }
            lblTongTien.Text = tongTien + " VND";
        }

        private void FillKhachHang()
        {
            cbKhachHang.Properties.DataSource = khachBL.GetData();
            cbKhachHang.Properties.ValueMember = "MaKhach";
            cbKhachHang.Properties.DisplayMember = "HoTen";
        }

        private void FillHangHoa()
        {
            cbHangHoa.Properties.DataSource = hangBL.GetData();
            cbHangHoa.Properties.ValueMember = "MaHang";
            cbHangHoa.Properties.DisplayMember = "TenHang";
        }

        private void btnSelectKhach_Click(object sender, EventArgs e)
        {
            int MaKhach = (int)cbKhachHang.EditValue;
            DMKhach obj = new DMKhach();
            obj.MaKhach = MaKhach;

            DataTable dt = khachBL.searchIDDMKhach(obj);
            lblDiaChi.Text = dt.Rows[0]["DiaChi"].ToString();
            lblDienThoai.Text = dt.Rows[0]["DienThoai"].ToString();
            lblEmail.Text = dt.Rows[0]["Email"].ToString();
            lblMaKhach.Text = dt.Rows[0]["MaKhach"].ToString();
            lblTenKhach.Text = dt.Rows[0]["HoTen"].ToString();
            lblNgayHD.Text = DateTime.Now.ToShortDateString();
        }

        private void btnAddKhachHang_Click(object sender, EventArgs e)
        {
            DMKhach objKhach = new DMKhach();

            using (frmDMKhachForm frm = new frmDMKhachForm())
            {
                if (frm.ShowDialog(this.FindForm()) == DialogResult.OK)
                {
                    objKhach.HoTen = frm.HoTen;
                    objKhach.DiaChi = frm.DiaChi;
                    objKhach.DienThoai = frm.DienThoai;
                    objKhach.Email = frm.Email;

                    khachBL.insertDMKhach(objKhach);
                    XtraMessageBox.Show("Đã thêm người dùng thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FillKhachHang();

                    cbKhachHang.ItemIndex = 0;
                    btnSelectKhach_Click(sender, e);
                }
            }
        }

        private void btnThanhToan_Click(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                HoaDonBan obj = new HoaDonBan();
                obj.MaKhach = int.Parse(lblMaKhach.Text);
                obj.NgHD = DateTime.Now;
                obj.DienGiai = txtDienGiai.Text;
                obj.Duyet = true;
                obj.NgDuyet = DateTime.Now;

                HoaDonBanBL hoaDonBanBl = new HoaDonBanBL();
                hoaDonBanBl.insertHoaDonBan(obj);

                int MaHoaDon = hoaDonBanBl.GetMaHoaDonNew();
                ChiTietHoaDonBL chiTietHoaDonBl = new ChiTietHoaDonBL();
                foreach (DataRow r in dtCart.Rows)
                {
                    ChiTietHoaDon chiTietHoaDon = new ChiTietHoaDon();
                    chiTietHoaDon.MaHang = int.Parse(r["MaHang"].ToString());
                    chiTietHoaDon.MaHD = MaHoaDon;
                    chiTietHoaDon.DonGia = float.Parse(r["DonGia"].ToString());
                    chiTietHoaDon.SoLuong = int.Parse(r["SoLuong"].ToString());

                    chiTietHoaDonBl.insertChiTietHoaDon(chiTietHoaDon);
                }

                ResetForm();
                XtraMessageBox.Show("Thanh toán tiền thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ResetForm()
        {
            FillHangHoa();
            FillKhachHang();

            dtCart = new DataTable();
            dtCart.Columns.Add("MaHang");
            dtCart.Columns.Add("TenHang");
            dtCart.Columns.Add("DVT");
            dtCart.Columns.Add("MaNhaCungCap");
            dtCart.Columns.Add("MaLoaiHang");
            dtCart.Columns.Add("DonGia");
            dtCart.Columns.Add("SoLuong");
            dtCart.Columns.Add("ThanhTien");

            lblDiaChi.Text = "";
            lblDienThoai.Text = "";
            lblEmail.Text = "";
            lblMaKhach.Text = "";
            lblTenKhach.Text = "";
            lblTongTien.Text = "";
            txtDienGiai.Text = "";
        }

        private bool ValidateForm()
        {
            if (dtCart.Rows.Count <= 0)
            {
                XtraMessageBox.Show("Chưa chọn mặt hàng cần thanh toán", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (lblMaKhach.Text == "")
            {
                XtraMessageBox.Show("Chưa chọn khách hàng cần thanh toán", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            ResetForm();
        }
    }
}